/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.supplierapply.service.impl.SupplierApplyServiceImpl
   Module Description   :

   Date Created      : 2009/12/3
   Original Author   : andy.chuang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.supplierapply.service.impl;

import java.io.ByteArrayOutputStream;

import javax.annotation.Resource;

import org.apache.commons.betwixt.io.BeanWriter;
import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.batch.service.BatchJobService;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.ec.product.service.ProductService;
import com.yaodian100.ec.supplierapply.entity.SupplierApply;
import com.yaodian100.ec.supplierapply.service.SupplierApplyService;

/**
 * @author andy.chuang
 * 
 */
public class SupplierApplyServiceImpl extends DomainServiceImpl<SupplierApply> implements SupplierApplyService {
	@Resource
	private BatchJobService batchJobService;
	@Resource
	private BaseManager baseMgr;
	@Resource
	private ProductService productservice;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.supplierapply.service.SupplierApplyService#save(java.lang.Object)
	 */
	@Override
	@Transactional(readOnly = false)
	public SupplierApply save(SupplierApply sa) throws CoreException {
		// sa=super.save(sa);
		// batchJobService.save("SupplierApplySyncUpdateJob", batchJobService.toJSON(sa), "S", null, null);

		return sa;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.supplierapply.service.SupplierApplyService#update(java.lang.Object)
	 */
	@Override
	@Transactional(readOnly = false)
	public SupplierApply update(SupplierApply sa) throws CoreException {
		sa = super.update(sa);
		if (sa.getApproveStatus() >= 1) {
			batchJobService.save("EC-SupplierApplySyncUpdateJob", toXML(sa), null, sa.getSupplierID());
		}

		return sa;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaodian100.ec.supplierapply.service.SupplierApplyService#saveOrUpdate(com.yaodian100.ec.supplierapply.entity
	 * .SupplierApply)
	 */
	@Override
	@Transactional(readOnly = false)
	public void saveOrUpdate(SupplierApply sa) throws CoreException {
		super.getDao().saveOrUpdate(sa);
		if ("N".equals(sa.getIsScm())) {
			productservice.updateIsScmToBOBatch(sa.getSupplierID());
		}
		if (sa.getApproveStatus() >= 1) {
			batchJobService.save("EC-SupplierApplySyncUpdateJob", toXML(sa), null, sa.getSupplierID());
		}
	}

	private String toXML(SupplierApply entity) throws CoreException {
		String xml = null;
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			BeanWriter beanWriter = new BeanWriter(baos);
			beanWriter.enablePrettyPrint();
			beanWriter.write(entity);
			xml = baos.toString();
			beanWriter.flush();
			baos.flush();
			beanWriter.close();
			baos.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw new CoreException("to xml fail", e);
		}
		return xml;
	}

	@Transactional(readOnly = false)
	public Object[] getPMApprovedSupplier(int start, int max) {
		Object[] objs = baseMgr.queryByHql("from SupplierApply where approveStatus=? and status=?", new Object[] {
				SupplierApply.APPROVE_APPROVED, SupplierApply.ACTIVE }, start, max);
		return objs;
	}

}
